var younight = switchesStorage.getSwitch( 'nightMode' ),
	yousign = switchesStorage.getSwitch( 'signSwitch' );

// Insert two style elements for later use.
var sty1 = $( '<link>' ).attr( 'rel', 'stylesheet' ).attr( 'data-ext', '_BDB_color_' );

var sty2 = $( '<style>' ).attr( 'data-ext', '_BDB_style_' );
$( document.documentElement ).append( sty1, sty2 );

Config.fInit( onSettingsGet );

function onSettingsGet( ret ) {

	'use strict';

	$.fireEvent( 'setting-got-from-bgpage', ret );

	// Local shortcut.
	var getConfig = Config.get;

	/** @type {Array.<string>} */
	var cssHideSelectors = [];

	// Block signatures if need.
	if ( getConfig( 'signSwitch' ) && yousign && /[pcf]/.test( pathhead ) ) {
		$.blockBySrc( /\?v=tbs$/ );
		cssHideSelectors = [ '.d_sign_split','.d_sign_split+img','.d_sign_spacer','.sign_split','.sign_split+img' ];
	}

	// Set style1.
	if ( getConfig('nightMode') && younight ) {
		sty1.attr( 'href', $c.getURL('css-set/night.css') );
	} else if ( getConfig('theme-color') ) {
		var _href = 'data:,';
		switch ( getConfig('theme-color') ) {
			case 'BDB' :
				_href = $c.getURL('css-set/day.css');
				break;
			case 'blue-white' :
				_href = $c.getURL('css-set/blue-white.css');
				break;
		}
		sty1.attr( 'href', _href );
	}

	var sty2CSS = '';
	if ( getConfig('theme-style') && ret.mainCSS ) {
		sty2CSS += ret.mainCSS;
	}

	if (cssHideSelectors.length) {
		sty2CSS += cssHideSelectors.join(',') + '{display:none!important}';
	}

	sty2.text( sty2CSS );


	// Single tab browse mode.
	if ( getConfig( 'singleTab' ) ) {
		$( document ).mousedown( function(e) {
			"use strict";
			var link = $( e.target );
			if ( link.prop( 'tagName') !== 'A' || !link.prop( 'host' ) )
				return;
			if ( link.prop( 'host' ) !== location.host )
				return;

			if ( link.attr( 'target' ) === '_blank' && self === top )
				link.removeAttr( 'target' );
		});
	}

	// Update foreground cache.
	if ( ret.status !== 304 ) {
		localStorage['__BDB_lastModified__'] = $.now();
		localStorage['__BDB_configSetting__'] = ret['setting'];
	}

}
